using AutoMapper;
using B.S.Sterilizationcenter.Domain.yjb;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Read.API.Appliaction.Commends.yjb;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Read.API.Appliaction.CommandHandlers.yjb
{
    public class ShowMedicineInventoryCommandHandler : IRequestHandler<ShowMedicineInventoryCommand, ApiResult<List<MedicineInventory>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public ShowMedicineInventoryCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 查看已发药品
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<MedicineInventory>>> Handle(ShowMedicineInventoryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<MedicineInventory>> result = new ApiResult<List<MedicineInventory>>();
                var list = db.Queryable<MedicineInventory>().Where(x => x.IsDeleted == false).ToList();

                if (request.MedicineInventoryId.HasValue)
                {
                    list = list.Where(it => it.MedicineInventoryId == request.MedicineInventoryId).ToList();
                }
                if (!string.IsNullOrEmpty(request.CardNo))
                {
                    list = list.Where(it => it.CardNo.Contains(request.CardNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.Name))
                {
                    list = list.Where(it => it.Name.Contains(request.Name)).ToList();
                }
                if (!string.IsNullOrEmpty(request.DispenseStatus))
                {
                    list = list.Where(it => it.DispenseStatus.Contains(request.DispenseStatus)).ToList();
                }
                if (!string.IsNullOrEmpty(request.DateRange))
                {
                    // 根据日期范围进行筛选
                    DateTime now = DateTime.Now;
                    DateTime startDate = DateTime.Now;
                    DateTime endDate = DateTime.Now;

                    switch (request.DateRange)
                    {
                        case "今天":
                            startDate = now.Date;
                            endDate = now.Date.AddDays(1).AddSeconds(-1);
                            break;
                        case "本周":
                            startDate = now.AddDays(-(int)now.DayOfWeek + 1).Date;
                            endDate = startDate.AddDays(7).AddSeconds(-1);
                            break;
                        case "本月":
                            startDate = new DateTime(now.Year, now.Month, 1);
                            endDate = startDate.AddMonths(1).AddSeconds(-1);
                            break;
                        case "本年":
                            startDate = new DateTime(now.Year, 1, 1);
                            endDate = startDate.AddYears(1).AddSeconds(-1);
                            break;
                        case "全部":
                            // 不做过滤，返回所有数据
                            break;
                    }

                    if (request.DateRange != "全部")
                    {
                        list = list.Where(it => it.DispenseTime >= startDate && it.DispenseTime <= endDate).ToList();
                    }
                }

                result.Code = ApiEnums.查询成功;
                result.Msg = "查询成功";
                result.Data = list;
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
} 